﻿using System.Collections;
using System.Collections.Generic;
using System.Security.AccessControl;
using UnityEditor;
using UnityEngine;

namespace Main
{
    [CustomEditor(typeof(RemotePrefabsConfig))]
    public class RemotePrefabsConfigEditor : Editor
    {
        RemotePrefabsConfig config;
        Transform transform;
        bool isFoldout;
        private void OnEnable()
        {
            config = (RemotePrefabsConfig)target;
            transform = config.transform;
        }
        public override void OnInspectorGUI()
        {
            //base.OnInspectorGUI();
            GUILayout.Box("用于加载AB中的预制件，减少包体大小。根据相机距离优先加载。打包时需移除节点下的预制件对象，并确保场景没有使用这些预制件资源！");

            isFoldout = EditorGUILayout.BeginFoldoutHeaderGroup(isFoldout, "动态加载预制件个数：" + config.remotePrefabsConfigs.Count);
            if (isFoldout)
            {
                using (new EditorGUI.DisabledScope(true))
                {

                    for (int i = 0; i < config.remotePrefabsConfigs.Count; i++)
                    {
                        GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(config.remotePrefabsConfigs[i].assetPath);
                        EditorGUILayout.ObjectField(go, typeof(GameObject), false);
                    }
                }
            }
            EditorGUILayout.EndFoldoutHeaderGroup();
            config.isAutoLoad = GUILayout.Toggle(config.isAutoLoad, "是否自动加载AB包预制件对象！");
            config.firstDelay = EditorGUILayout.Slider("延迟自动加载预制件时间(s)：", config.firstDelay, 0, 100);
            config.intervalDelay = EditorGUILayout.Slider("预制件间隔延迟加载时间(s)：", config.intervalDelay, 0, 10);
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("更新配置"))
            {
                SaveConfig();
            }
            if (GUILayout.Button("清空配置"))
            {
                config.remotePrefabsConfigs.Clear();
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("移除预制件实例化"))
            {
                RemoveChildren();
            }
            if (GUILayout.Button("还原预制件实例化"))
            {
                RevertChildren();
            }
            EditorGUILayout.EndHorizontal();
        }
        private void SaveConfig()
        {
            Transform child = null;
            config.remotePrefabsConfigs.Clear();
            for (int i = 0; i < transform.childCount; i++)
            {
                child = transform.GetChild(i);
                if (PrefabUtility.IsPartOfPrefabInstance(child))
                {
                    GameObject assetPrefab = PrefabUtility.GetCorrespondingObjectFromOriginalSource(child.gameObject);

                    if (assetPrefab != null)
                    {
                        RemotePrefabItem item = new RemotePrefabItem();
                        item.assetPath = AssetDatabase.GetAssetPath(assetPrefab);
                        item.assetGUID = AssetDatabase.AssetPathToGUID(item.assetPath);
                        item.name = child.name;
                        item.isStatic = child.gameObject.isStatic;
                        item.tag = child.tag;
                        item.layer = child.gameObject.layer;
                        item.position = child.position;
                        item.rotation = child.rotation;
                        item.scale = child.localScale;
                        config.remotePrefabsConfigs.Add(item);
                    }
                    else
                    {
                        Debug.LogWarning(child.name + ":这个预制件找不到所在目录！");
                    }
                }
            }
        }
        private void RemoveChildren()
        {
            GameObject child = null;
            for (int i = 0; i < transform.childCount;)
            {
                child = transform.GetChild(i).gameObject;
                if (PrefabUtility.IsPartOfPrefabInstance(child))
                {
                    Undo.DestroyObjectImmediate(child);
                    DestroyImmediate(child);
                }
                else
                {
                    i++;
                }
            }
        }
        private void RevertChildren()
        {
            for (int i = 0; i < config.remotePrefabsConfigs.Count; i++)
            {
                RemotePrefabItem item = config.remotePrefabsConfigs[i];
                string path = AssetDatabase.GUIDToAssetPath(item.assetGUID);
                GameObject assetObj = AssetDatabase.LoadAssetAtPath<GameObject>(path);
                if (assetObj != null)
                {
                    GameObject child = PrefabUtility.InstantiatePrefab(assetObj) as GameObject;
                    child.transform.parent = transform;
                    child.name = item.name;
                    child.isStatic = item.isStatic;
                    child.tag = item.tag;
                    child.layer = item.layer;
                    child.transform.position = item.position;
                    child.transform.rotation = item.rotation;
                    child.transform.localScale = item.scale;
                    Undo.RegisterCreatedObjectUndo(child, "RevertChildren");
                }
                else
                {
                    Debug.LogWarning(item.name + "这个配置丢失资源对象！");
                }
            }
        }
    }
}